package net.projecteuler.prob;

import java.math.BigInteger;

public class Problem55 implements Runnable {

	@Override
	public void run() {
		int cnt = 0;
		
		for(int i = 1; i < 10000; i++){
			BigInteger big1 = new BigInteger(Integer.toString(i));
			String str = big1.toString();
			
			boolean isLychrel = true;
			for(int j = 0; j < 50; j++){
				String rev = reverse(str);
				BigInteger big2 = new BigInteger(rev);
				
				big1 = big1.add(big2);
				str = big1.toString();
				if(isPanlidrome(str)){
					isLychrel = false;
					break;
				}	
			}
			
			if(isLychrel){
				System.out.println(i);
				cnt++;
			}
		}
		
		System.out.println(cnt);
	}
	
	private String reverse(String str){
		StringBuilder sb = new StringBuilder(str.length());
		for(int i = str.length() - 1; i >= 0; i--)
			sb.append(str.charAt(i));		
		return sb.toString();
	}
	
	private boolean isPanlidrome(String str){
		int length = str.length();
		for(int i = 0; i < length / 2; i++){
			if(str.charAt(i) != str.charAt(length - 1 - i))
				return false;
		}
		return true;
	}

}
